package cn.xfdzx.android.apps.shop.activity;

import android.app.Dialog;
import android.content.Intent;
import android.net.Uri;
import android.view.LayoutInflater;
import android.view.View;
import android.widget.ImageView;
import android.widget.LinearLayout;
import android.widget.RelativeLayout;
import android.widget.TextView;
import android.widget.Toast;

import androidx.recyclerview.widget.GridLayoutManager;
import androidx.recyclerview.widget.LinearLayoutManager;
import androidx.recyclerview.widget.RecyclerView;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import cn.xfdzx.android.apps.shop.R;
import cn.xfdzx.android.apps.shop.adapter.AfterServiceInfogoodsMarketAdapter;
import cn.xfdzx.android.apps.shop.adapter.AfterServiceProcessGoodsListAdapter;
import cn.xfdzx.android.apps.shop.adapter.AfterServiceProcessImageListAdapter;
import cn.xfdzx.android.apps.shop.adapter.AfterServiceProcessListAdapter;
import cn.xfdzx.android.apps.shop.adapter.AfterServiceProcessListMarketAdapter;
import cn.xfdzx.android.apps.shop.adapter.FullyGridLayoutManager;
import cn.xfdzx.android.apps.shop.apps.Constants;
import cn.xfdzx.android.apps.shop.base.BaseMvpActivity;
import cn.xfdzx.android.apps.shop.bean.AfterMarketInfoBean;
import cn.xfdzx.android.apps.shop.bean.AfterServiceUpLoadMoreImgBean;
import cn.xfdzx.android.apps.shop.bean.AfterServiceUpLoadSingImgBean;
import cn.xfdzx.android.apps.shop.bean.OrderDetailInfoBeen;
import cn.xfdzx.android.apps.shop.bean.OrderListBeen;
import cn.xfdzx.android.apps.shop.bean.OrderProcessBeen;
import cn.xfdzx.android.apps.shop.bean.ReturnReasonBean;
import cn.xfdzx.android.apps.shop.contract.AfterServiceContract;
import cn.xfdzx.android.apps.shop.presenter.AfterServicePresenter;
import cn.xfdzx.android.apps.shop.util.EmptyUtil;
import cn.xfdzx.android.apps.shop.util.ProgressDialog;
import cn.xfdzx.android.apps.shop.util.Utils;

/*
/* 售后退款详情页面
 */
public class AfterServiceInfoMarketActivity extends BaseMvpActivity<AfterServicePresenter> implements AfterServiceContract.View , View.OnClickListener{
    private RecyclerView recycler_goods;//商品列表
    private RecyclerView recycler_imgs;//列表图片
    private RecyclerView recycler_process;//进度列表
    private TextView tv_returnmoney;//退款金额

    private TextView tv_returnreason;//问题类型
    private TextView tv_returndes;//问题
    private ImageView iv_back;
    private TextView tv_order_num;
    private TextView tv_fuzhi;
    private TextView tv_order_time;
    private TextView tv_pay_time;
    private TextView tv_the_freight_price;//运费
    private TextView tv_kefu;//客服
    private TextView tv_call;//电话
    private TextView tv_state;//售后信息状态
    private TextView tv_content_title;//绿色状态头
    private TextView tv_time_length;//绿色剩余时间
    private ImageView img_state;
    private TextView tv_cancle;//取消申请
    private TextView tv_again;//重新提交
    private RelativeLayout rl_return;//退款成功显示布局
    private TextView tv_refoundmoney;//退款成功显示金额
    private TextView tv_tui;//退款渠道

    private AfterServiceInfogoodsMarketAdapter afterServiceGoodsListAdapter;
    private String orderId;//订单id
    private String applyId;//申请售后的ID
    private List<AfterMarketInfoBean.DataBean.GoodsBean> goods = new ArrayList<>();//商品列表
    private AfterServiceProcessImageListAdapter imageListAdapter;
    private List<String> refund_reason_wap_img;
    private Dialog dialog;//确认弹窗
    private AfterServiceProcessListMarketAdapter processListMarketAdapter;//进度
    private List<String> msg = new ArrayList<>();
    private OrderListBeen.DataBean dataBean;//订单原数据用于详情页面重新提交
    private String customer_service_phone,store_phone;
    @Override
    public int getLayoutId() {
        return R.layout.activity_xfdafterorder_detail;
    }

    @Override
    public void initView() {
        mPresenter = new AfterServicePresenter();
        mPresenter.attachView(this);
        orderId = getIntent().getStringExtra("orderId");
        applyId = getIntent().getStringExtra("applyId");
        dataBean = (OrderListBeen.DataBean) getIntent().getSerializableExtra("data");
        recycler_goods = findViewById(R.id.recycler_goods);
        recycler_imgs = findViewById(R.id.recycler_imgs);
        recycler_process = findViewById(R.id.recycler_process);
        tv_returnmoney = findViewById(R.id.tv_returnmoney);

        tv_returnreason = findViewById(R.id.tv_returnreason);
        tv_returndes = findViewById(R.id.tv_returndes);
        //tv_cancleafter = findViewById(R.id.tv_cancleafter);
        iv_back = findViewById(R.id.iv_back);

        //tv_cancleafter.setOnClickListener(this);

        tv_order_num = findViewById(R.id.tv_order_num);
        tv_fuzhi = findViewById(R.id.tv_fuzhi);
        tv_order_time = findViewById(R.id.tv_order_time);
        tv_pay_time = findViewById(R.id.tv_pay_time);
        tv_the_freight_price = findViewById(R.id.tv_the_freight_price);
        tv_kefu = findViewById(R.id.tv_kefu);
        tv_call = findViewById(R.id.tv_call);
        tv_state = findViewById(R.id.tv_state);
        tv_content_title = findViewById(R.id.tv_content_title);
        tv_time_length = findViewById(R.id.tv_time_length);
        img_state = findViewById(R.id.img_state);
        tv_cancle = findViewById(R.id.tv_cancle);
        tv_again = findViewById(R.id.tv_again);
        rl_return = findViewById(R.id.rl_return);
        tv_refoundmoney = findViewById(R.id.tv_refoundmoney);
        tv_tui = findViewById(R.id.tv_tui);
        iv_back.setOnClickListener(this);
        tv_fuzhi.setOnClickListener(this);
        tv_cancle.setOnClickListener(this);
        tv_again.setOnClickListener(this);
        tv_kefu.setOnClickListener(this);
        tv_call.setOnClickListener(this);

        Map<String,Object> map = new HashMap<>();
        map.put("applyId",applyId);
        mPresenter.getMarketAfterServiceInfo(map);
        initListener();
    }

    private void initListener() {
        LinearLayoutManager layoutManagermsg = new LinearLayoutManager(this,LinearLayoutManager.VERTICAL,false);
        recycler_process.setLayoutManager(layoutManagermsg);
        processListMarketAdapter = new AfterServiceProcessListMarketAdapter(msg);
        recycler_process.setAdapter(processListMarketAdapter);

        LinearLayoutManager layoutManager = new LinearLayoutManager(this,LinearLayoutManager.VERTICAL,false);
        recycler_goods.setLayoutManager(layoutManager);
        afterServiceGoodsListAdapter = new AfterServiceInfogoodsMarketAdapter(goods);
        recycler_goods.setAdapter(afterServiceGoodsListAdapter);

        FullyGridLayoutManager manager = new FullyGridLayoutManager(this, 4, GridLayoutManager.VERTICAL, false);
        recycler_imgs.setLayoutManager(manager);
        imageListAdapter = new AfterServiceProcessImageListAdapter(refund_reason_wap_img);
        recycler_imgs.setAdapter(imageListAdapter);

        dialog = Utils.addDialog(this, R.layout.dialog, R.string.confirmcancleafter, R.string.confirm, new View.OnClickListener() {
            @Override
            public void onClick(View view) {
                Map<String,Object> map = new HashMap<>();
                map.put("orderId",orderId);
                map.put("applyId",applyId);
                mPresenter.getMarketAfterServiceCancle(map);
                dialog.dismiss();
            }
        });

//        processListAdapter = new AfterServiceProcessListAdapter(progress);
//        recycler_process.setLayoutManager(new LinearLayoutManager(this));
//        recycler_process.setAdapter(processListAdapter);
    }
    @Override
    public void showLoading() {
        if (!ProgressDialog.getInstance().materialDialog.isShowing()) {
            ProgressDialog.getInstance().show(this);
        }

    }

    @Override
    public void hideLoading() {
        ProgressDialog.getInstance().dismiss();
    }

    @Override
    public void onError(Throwable throwable) {

    }

    /**上传单张回调*/
    @Override
    public void onUploadMoreImgSuccess(AfterServiceUpLoadMoreImgBean bean) {

    }

    /**上传单张回调*/
    @Override
    public void onUploadSingImgSuccess(AfterServiceUpLoadSingImgBean bean) {

    }

    @Override
    public void getReturnReasonSuccess(ReturnReasonBean bean) {
    }

    @Override
    public void getAfterServiceRequestSuccess(OrderProcessBeen bean) {

    }

    @Override
    public void getAfterServiceCancleSuccess(ReturnReasonBean bean) {

    }

    @Override
    public void getAfterServiceProgressSuccess(OrderProcessBeen bean) {

    }

    @Override
    public void getOrderDetailSuccess(OrderDetailInfoBeen bean) {

    }

    @Override
    public void getMarketAfterServiceRequestSucces(OrderProcessBeen bean) {

    }

    @Override
    public void getMarketAfterServiceCancleSucces(ReturnReasonBean bean) {
        if (bean.getStatus() == Constants.SUCCESS_CODE) {
            Toast.makeText(this, "已取消售后", Toast.LENGTH_SHORT).show();
            Map<String,Object> map = new HashMap<>();
            map.put("applyId",applyId);
            mPresenter.getAfterServiceProgress(map);
        } else {
            Toast.makeText(this, bean.getMsg(), Toast.LENGTH_SHORT).show();
        }
    }

    @Override
    public void getMarketAfterServiceInfoSucces(AfterMarketInfoBean bean) {
        if (bean.getStatus() == Constants.SUCCESS_CODE) {
            if (EmptyUtil.isNotEmpty(bean.getData())){

                tv_returnreason.setText("退款原因: "+bean.getData().getApply().getReason());
                if (EmptyUtil.isNotEmpty(bean.getData().getApply().getDesc())){
                    tv_returndes.setText("退款说明: "+bean.getData().getApply().getDesc());
                }else {
                    tv_returndes.setText("退款说明: ");
                }
                tv_returnmoney.setText("退款总金额: ¥"+bean.getData().getApply().getApply_price());
                tv_order_num.setText("订单编号: "+bean.getData().getApply().getOrder_id());
                tv_order_time.setText("创建时间: "+bean.getData().getApply().getCreated_at());
                tv_pay_time.setText("付款时间: "+bean.getData().getApply().getPay_time());
                tv_the_freight_price.setText("¥0");
                afterServiceGoodsListAdapter.refreshDatas(bean.getData().getGoods());

                customer_service_phone = bean.getData().getApply().getCustomer_service_phone();
                store_phone = bean.getData().getApply().getStore_phone();
                if (EmptyUtil.isNotEmpty(bean.getData().getStatus())){
                   // 申请的状态  1 => 申请中  2 => 平台同意申请售后  3 => 平台拒绝申请  4 => 用户取消申请售后 5 => 退款中 6 => 退款成功
                    tv_content_title.setText(bean.getData().getStatus().getTitle().getMsg());
                    tv_time_length.setText(bean.getData().getStatus().getTitle().getRemaining_time());
                    if (EmptyUtil.isNotEmpty(bean.getData().getStatus().getMsg().getMsg())){
                        processListMarketAdapter.refreshDatas(bean.getData().getStatus().getMsg().getMsg());
                    }else {
                        recycler_process.setVisibility(View.GONE);
                    }

                    switch (String.valueOf(bean.getData().getStatus().getStatus())){
                        case "1":
                            img_state.setImageResource(R.drawable.ic_refund);
                            tv_state.setText(bean.getData().getStatus().getMsg().getStatus_title());
                            tv_cancle.setVisibility(View.VISIBLE);
                            break;
                        case "3":
                            img_state.setImageResource(R.drawable.ic_refoundreturn);
                            tv_state.setText("拒绝原因："+bean.getData().getApply().getRefuse_reason());
                            tv_again.setVisibility(View.VISIBLE);
                            break;
                        case "4":
                            recycler_process.setVisibility(View.GONE);
                            img_state.setImageResource(R.drawable.ic_refoundcancle);
                            tv_state.setText(bean.getData().getStatus().getMsg().getStatus_title());

                            break;
                        case "2":
                        case "5":
                        case "6":
                            rl_return.setVisibility(View.VISIBLE);
                            recycler_process.setVisibility(View.GONE);
                            tv_state.setVisibility(View.GONE);
                            tv_cancle.setVisibility(View.GONE);
                            img_state.setImageResource(R.drawable.ic_refoundsucc);
                            // 支付的类型  1=> 微信支付  2=> 支付宝支付  3 => 农行支付
                            tv_refoundmoney.setText("¥"+bean.getData().getApply().getApply_price());
                            if (bean.getData().getApply().getPay_type() == 1){
                                tv_tui.setText("退回微信");
                            }else if (bean.getData().getApply().getPay_type() == 2){
                                tv_tui.setText("退回支付宝");
                            }else if (bean.getData().getApply().getPay_type() == 3){
                                tv_tui.setText("退回农行卡");
                            }else {
                                tv_tui.setText("退回原支付渠道");
                            }
                            break;
                    }
                }

                refund_reason_wap_img = bean.getData().getApply().getImgs();
                imageListAdapter.refreshDatas(refund_reason_wap_img);
            }

        } else {
            Toast.makeText(this, bean.getMsg(), Toast.LENGTH_SHORT).show();
        }
    }

    @Override
    protected void onDestroy() {
        super.onDestroy();
        if (null!=dialog){
            dialog.dismiss();
        }
        if (null!=mPresenter)
        mPresenter.detachView();
    }

    @Override
    public void onClick(View view) {
        switch (view.getId()) {
            case R.id.iv_back:    //返回按钮点击
                finish();
                break;
            case R.id.tv_cancle:    //取消申请
                dialog.show();
                break;
            case R.id.tv_again:    //重新提交
                Intent intent = new Intent(this,AfterServiceMaraketGoodsActivity.class);
                intent.putExtra("data", dataBean);
                startActivity(intent);
                finish();
                break;
            case R.id.tv_fuzhi:
                Utils.copy(this, tv_order_num.getText().toString());
                Toast.makeText(this, "复制成功", Toast.LENGTH_SHORT).show();
                break;
            case R.id.tv_kefu:
                callPhone(customer_service_phone);
                break;
            case R.id.tv_call:
                if (EmptyUtil.isNotEmpty(store_phone)){
                    callPhone(store_phone);
                }else {
                    callPhone(customer_service_phone);
                }
                break;
        }
    }

    /**
     * 拨打电话（跳转到拨号界面，用户手动点击拨打）
     *
     * @param phoneNum 电话号码
     */
    public  void callPhone(String phoneNum) {
        Intent intent = new Intent(Intent.ACTION_DIAL);
        Uri data = Uri.parse("tel:" + phoneNum);
        intent.setData(data);
        startActivity(intent);
    }
}
